package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.OrderContractMapper;
import com.ruoyi.crm.domain.OrderContract;
import com.ruoyi.crm.service.IOrderContractService;

/**
 * 合同订单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-27
 */
@Service
public class OrderContractServiceImpl implements IOrderContractService
{
    @Autowired
    private OrderContractMapper orderContractMapper;

    /**
     * 查询合同订单
     *
     * @param id 合同订单主键
     * @return 合同订单
     */
    @Override
    public OrderContract selectOrderContractById(String id)
    {
        return orderContractMapper.selectOrderContractById(id);
    }

    /**
     * 查询合同订单列表
     *
     * @param orderContract 合同订单
     * @return 合同订单
     */
    @Override
    public List<OrderContract> selectOrderContractList(OrderContract orderContract)
    {
        return orderContractMapper.selectOrderContractList(orderContract);
    }

    /**
     * 新增合同订单
     *
     * @param orderContract 合同订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrderContract(OrderContract orderContract)
    {
        int rows = orderContractMapper.insertOrderContract(orderContract);
        insertCustomerProduct(orderContract);
        return rows;
    }

    /**
     * 修改合同订单
     *
     * @param orderContract 合同订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrderContract(OrderContract orderContract)
    {
        orderContract.setUpdateTime(DateUtils.getNowDate());
        orderContractMapper.deleteCustomerProductByCustomerName(orderContract.getId());
        insertCustomerProduct(orderContract);
        return orderContractMapper.updateOrderContract(orderContract);
    }

    /**
     * 批量删除合同订单
     *
     * @param ids 需要删除的合同订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderContractByIds(String[] ids)
    {
        return orderContractMapper.deleteOrderContractByIds(ids);
    }

    /**
     * 删除合同订单信息
     *
     * @param id 合同订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderContractById(String id)
    {
        orderContractMapper.deleteCustomerProductByCustomerName(id);
        return orderContractMapper.deleteOrderContractById(id);
    }

    @Override
    public OrderContract getContractByNo(String contractNo) {
        return orderContractMapper.getContractByNo(contractNo);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param orderContract 合同订单对象
     */
    public void insertCustomerProduct(OrderContract orderContract)
    {
        List<CustomerProduct> customerProductList = orderContract.getCustomerProductList();
        String id = orderContract.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                orderContractMapper.batchCustomerProduct(list);
            }
        }
    }
}